#include "TaskManager.hpp"

#include <time.h>
#include <thread>
#include <sstream>
#include "cybertron/core/UtilTime.hpp"
#include "TheApp.hpp"


TaskInfo::TaskInfo() {
	mTaskState = Common::ETaskState_None;
}
TaskInfo::~TaskInfo() {
	//clearAllNode();
}
void TaskInfo::setTaskState(Common::ETaskState taskState) {

	if (mTaskState == taskState) {
		//logOutputWarn("setTaskState is same %d", taskState);
		return;
	}
	if (mTaskState >= taskState) {
		return;
	}
	mTaskState = taskState;
	switch (taskState)
	{
	case Common::ETaskState_None:
		break;
	case Common::ETaskState_Ready:
		//onTaskReadyEvent();
		logInfo("===================> Task  Ready TaskId:%s <===================", mTaskId.c_str());
		break;
	case Common::ETaskState_Starting:
		logInfo("===================> Task  Starting TaskId:%s <===================", mTaskId.c_str());
		break;
	case Common::ETaskState_Started:
		logInfo("===================> Task  Started TaskId:%s <===================", mTaskId.c_str());
		//onTaskStartedEvent();
		break;
	case Common::ETaskState_Running:
		logInfo("===================> Task  Running TaskId:%s <===================", mTaskId.c_str());
		break;
	case Common::ETaskState_Ending:
		logInfo("===================> Task  Ending TaskId:%s <===================", mTaskId.c_str());
		break;
	case Common::ETaskState_Ended:
		logInfo("===================> Task  Ended TaskId:%s <===================", mTaskId.c_str());
		break;
	default:
		break;
	}

	
}

void TaskInfo::ForceInitTaskState() {
	mTaskState = Common::ETaskState_Starting;
	std::unique_lock<std::recursive_mutex> lock(mSyncWorkNodeDynamicConfigMapLock);
	mSyncWorkNodeDynamicConfigMap.clear();
}

void TaskInfo::addWorkDynamicConfig(Common::SyncWorkNodeDynamicConfig* pConfig) {
	std::unique_lock<std::recursive_mutex> lock(mSyncWorkNodeDynamicConfigMapLock);
	mSyncWorkNodeDynamicConfigMap[pConfig->worknodeid()] = *pConfig;
}

Common::SyncWorkNodeDynamicConfig* TaskInfo::getWorkDynamicConfig(string workNodeId, Common::EWorkNodeType workType) {
	std::unique_lock<std::recursive_mutex> lock(mSyncWorkNodeDynamicConfigMapLock);
	auto it = mSyncWorkNodeDynamicConfigMap.find(workNodeId);
	if (it != mSyncWorkNodeDynamicConfigMap.end()) {

		return &it->second;
	}
	for (auto nt = mSyncWorkNodeDynamicConfigMap.begin(); nt != mSyncWorkNodeDynamicConfigMap.end(); nt++)
	{
		if (nt->second.worknodetype() == workType)
		{
			return &nt->second;
		}
	}
	return 0;
}
TaskManager::TaskManager() {
}
TaskManager::~TaskManager() {
}
TaskInfo* TaskManager::getTaskInfo(string taskId) {
	std::unique_lock<std::recursive_mutex> lock(mTaskConfigInfoMapLock);
	auto it = mTaskInfoMap.find(taskId);
	if (it == mTaskInfoMap.end()) {

		return 0;
	}
	return it->second;
}

bool TaskManager::setTaskState(string task_id, Common::ETaskState State) {
	TaskInfo* pTask = getTaskInfo(task_id);
	if (!pTask) {
		return false;
	}
	pTask->setTaskState(State);
	return true;
}
bool TaskManager::getTaskState(string task_id, Common::ETaskState& State) {
	TaskInfo* pTask = getTaskInfo(task_id);
	if (!pTask) {
		return false;
	}
	State = pTask->getTaskState();
	return true;
}

bool TaskManager::addTask(string task_id, Master::TaskStartFromCommander* pTaskConfig) {
	std::unique_lock<std::recursive_mutex> lock(mTaskConfigInfoMapLock);
	auto it = mTaskInfoMap.find(task_id);
	if (it != mTaskInfoMap.end()) {
		it->second->mStartTaskConfig = *pTaskConfig;
		it->second->ForceInitTaskState();
		logWarning("Task Replay PlayBack!!!");
		return true;
	}
	//string xxx = pTaskConfig->worldconfigjson();
	TaskInfo* pInfo =new TaskInfo;
	pInfo->mTaskId = task_id;
	pInfo->mStartTaskConfig = *pTaskConfig;
	pInfo->setTaskState(Common::ETaskState_Starting);
	mTaskInfoMap[task_id] = pInfo;
	return true;
}
bool TaskManager::getTaskWorkJson(string task_id, string workId, string& worldJson, string& workJson) {
	TaskInfo* pTask = getTaskInfo(task_id);
	if (!pTask) {
		return false;
	}

	worldJson = pTask ->mStartTaskConfig.worldconfigjson();
	int size = pTask->mStartTaskConfig.workconfiglist().size();

	for (int i = 0; i < size; i++) {
		
		if (pTask->mStartTaskConfig.workconfiglist()[i].worknodeid() == workId) {
			workJson = pTask->mStartTaskConfig.workconfiglist()[i].workjson();
			return true;
		}
	}
	return false;
}

bool TaskManager::getTaskWorkConfigItem(string task_id, string workId, Master::ConfigWorkItem& pConfigItem) {
	TaskInfo* pTask = getTaskInfo(task_id);
	if (!pTask) {
		return false;
	}

	
	int size = pTask->mStartTaskConfig.workconfiglist().size();

	for (int i = 0; i < size; i++) {

		if (pTask->mStartTaskConfig.workconfiglist()[i].worknodeid() == workId) {
			pConfigItem = pTask->mStartTaskConfig.workconfiglist()[i];
			return true;
		}
	}
	return false;
}
Master::TaskStartFromCommander* TaskManager::getTaskStartFromCommander(string task_id) {
	TaskInfo* pTask = getTaskInfo(task_id);
	if (!pTask) {
		return 0;
	}
	return &pTask->mStartTaskConfig;
}

bool TaskManager::addSyncWorkNodeDynamicConfig(Common::SyncWorkNodeDynamicConfig* pConfig){
	TaskInfo* pTask = getTaskInfo(pConfig->taskid());
	if (!pTask) {
		return false;
	}
	pTask->addWorkDynamicConfig(pConfig);
	return true;

}

bool TaskManager::getSyncWorkNodeDynamicConfig(string task_id, string workNodeId, Common::EWorkNodeType workType, Common::SyncWorkNodeDynamicConfig* pConfig) {
	std::unique_lock<std::recursive_mutex> lock(mTaskConfigInfoMapLock);
	auto it = mTaskInfoMap.find(task_id);
	if (it == mTaskInfoMap.end()) {
		return false;
	}
	Common::SyncWorkNodeDynamicConfig* ret = it->second->getWorkDynamicConfig(workNodeId, workType);
	if (!ret) {
		return false;
	}
	*pConfig = *ret;
	return true;
}